

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.rca.dto.SwCkReportReviewExportDTO;
import com.zdkj.subway.rca.entity.SwCkReportReview;
import com.zdkj.subway.rca.service.SwCkReportReviewService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;


/**
 * 报表间数据关联审核
 *
 * @author cooze
 * @date 2020-10-27 23:07:56
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckreportreview")
@Api(value = "swckreportreview", tags = "数据关联性审核-报表间数据关联审核管理")
public class SwCkReportReviewController {

    private final SwCkReportReviewService swCkReportReviewService;


    @ApiOperation(value = "查询差异类型", notes = "查询差异类型")
    @GetMapping("/diffType")
    public R diffType() {
        return R.ok(swCkReportReviewService.diffType());
    }


    /**
     * 报表间数据关联审核 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "diffType", value = "差异类型", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "mainReportStartDate", value = "主报表开始日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "mainReportEndDate", value = "主报表结束日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "recordDiffStartDate", value = "记录开始日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "recordDiffEndDate", value = "记录结束日期：yyyy-MM-dd", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "startMoney", value = "开始差异金额", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "endMoney", value = "结束差异金额", dataTypeClass = String.class)
            }
    )
    @GetMapping("/page")
    public R getSwCkReportReviewPage(Page page, String diffType,
                                     String mainReportStartDate,
                                     String mainReportEndDate,
                                     String recordDiffStartDate,
                                     String recordDiffEndDate,
                                     BigDecimal startMoney,
                                     BigDecimal endMoney) {
        SwCkReportReview swCkReportReview = new SwCkReportReview();
        if (StringUtils.isNotEmpty(diffType)) {
            swCkReportReview.setDifferenceType(diffType);
        }
        boolean isContainMainReport = StringUtils.isNotEmpty(mainReportStartDate) && StringUtils.isNotEmpty(mainReportEndDate);
        boolean isContainRecordDiff = StringUtils.isNotEmpty(recordDiffStartDate) && StringUtils.isNotEmpty(recordDiffEndDate);
        boolean isContainMoney = ObjectUtils.isNotNull(startMoney) && ObjectUtils.isNotNull(endMoney);
        return R.ok(swCkReportReviewService.page(page,
                Wrappers.query(swCkReportReview).lambda()
                        .between(isContainMainReport, SwCkReportReview::getMainReportCreateTime, mainReportStartDate, mainReportEndDate)
                        .between(isContainRecordDiff, SwCkReportReview::getRecordDifferenceTime, recordDiffStartDate, recordDiffEndDate)
                        .between(isContainMoney, SwCkReportReview::getDifferenceAmount, startMoney, endMoney)
                )
        );
    }


    /**
     * 通过id查询报表间数据关联审核
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swCkReportReviewService.getById(id));
    }

    /**
     * 新增报表间数据关联审核
     *
     * @param swCkReportReview 报表间数据关联审核
     * @return R
     */
    @ApiOperation(value = "新增报表间数据关联审核", notes = "新增报表间数据关联审核")
    @SysLog("新增报表间数据关联审核")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('rca_swckreportreview_add')")
    public R save(@RequestBody SwCkReportReview swCkReportReview) {
        PigxUser pigxUser = SecurityUtils.getUser();
        swCkReportReview.setCreatorId(pigxUser.getId());
        swCkReportReview.setCreatorName(pigxUser.getNikeName());
        swCkReportReview.setCreateTime(LocalDateTime.now());
        return R.ok(swCkReportReviewService.save(swCkReportReview));
    }

    /**
     * 修改报表间数据关联审核
     *
     * @param swCkReportReview 报表间数据关联审核
     * @return R
     */
    @ApiOperation(value = "修改报表间数据关联审核", notes = "修改报表间数据关联审核")
    @SysLog("修改报表间数据关联审核")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('rca_swckreportreview_edit')")
    public R updateById(@RequestBody SwCkReportReview swCkReportReview) {
        return R.ok(swCkReportReviewService.updateById(swCkReportReview));
    }
    /**
     * 导出报表间数据关联审核
     * @param dto
     * @param response
     */
    @ApiOperation(value = "导出报表间数据关联审核", notes = "导出报表间数据关联审核")
    @SysLog("导出报表间数据关联审核")
    @PostMapping("/exportExcel")
    @PreAuthorize("@pms.hasPermission('rca_swckreportreview_export_excel')")
    public void exportExcel(@RequestBody SwCkReportReviewExportDTO dto, HttpServletResponse response) {
        this.swCkReportReviewService.exportExcel(dto, response);
    }

}
